<?php
/* SVN FILE: $Id: user.php 20 2009-05-02 13:50:00Z ake.svedin $ */
/**
 * Travian Alliance Coordinator
 * Copyright (c) 2009 Åke Svedin.
 *
 * This file is part of Travian Alliance Coordinator.
 *
 * Travian Alliance Coordinator is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Travian Alliance Coordinator is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Travian Alliance Coordinator.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author $Author: ake.svedin $
 * @copyright Copyright (c) 2009 Åke Svedin.
 * @version $Revision: 20 $
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3
 */
class User extends AppModel {

    var $name = 'User';
    function __construct() {
        parent::__construct();

        $this->validate = array (
        'id' => array(
            'rule' => 'blank',
            'on' => 'create'
            ),
        'active' => array('boolean'),
        'username' => array(
            'notEmpty' => array(
                'rule' => 'notempty',
                'allowEmpty' => false,
                'message' => 'This field must be filled in'
                ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'This username has already been taken'
                )
            ),
        'passwd' => array(
            'rule' => array('minlength', 6),
            'required' => true,
            'message' => 'Must be at least 6 characters long'
            ),
        'passwd_confirm' => array(
            'rule' => 'confirmPassword',
            'message' => 'Passwords do not match'
            ),
        'email' => array(
            'rule' => 'email',
            'allowEmpty' => false,
            'message' => 'Enter a valid e-mail address'
            ),
        'ip' => array('ip'),
        'tid' => array(
                'rule' => array('range', 0, 4),
                'message' => 'Invalid value'
            )
        );
    }

    function confirmPassword($data) {
        return ($data['passwd_confirm'] == $this->data['User']['passwd']);
    }

    function beforeSave() {
        if (isset($this->data['User']['passwd'])) {
            $this->data['User']['password'] =
            Security::hash($this->data['User']['passwd'], null, true);
            unset($this->data['User']['passwd']);
        }

        if (isset($this->data['User']['passwd_confirm'])) {
            unset($this->data['User']['passwd_confirm']);
        }
        return true;
    }

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasOne = array(
        'Hero' => array(
            'className' => 'Hero',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    var $hasMany = array(
        'Village' => array(
            'className' => 'Village',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    var $hasAndBelongsToMany = array(
        'Group' => array(
            'className' => 'Group',
            'joinTable' => 'groups_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'group_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        ),
        'Squad' => array(
            'className' => 'Squad',
            'joinTable' => 'squads_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'squad_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

}
?>